<?php
	session_start();
	$date = $_GET["date"];
	require_once("../templates/comun.php");

	$conexion=Obtener_Conexion();
	$sql=" SELECT agente, nombre FROM cxcagentes WHERE estatus_agt='A' ORDER BY  nombre ASC ";
		$res=pg_query($conexion,$sql)  or die ("Error $sql");
	$registros=pg_num_rows($res);
	$combo_agentes = "<select id='id_agente'> ";

	while($recod = pg_fetch_array($res)){
		$combo_agentes .= "<option value='".$recod["agente"]."'>".$recod["agente"]." | ".$recod["nombre"]."</option>";
	}
	$combo_agentes .= "</select>";
	
	$cadena_datos = " grid_data = [ ";
	$cadena_datos .= " ]; ";
	
	Desconectar($conexion);
?>
	<script>
		var grid_data = null;
		var default_grid = null;
		var grid_detail = null;	

		function Buscar(){
			jQuery("#myGrid").jqGrid('setGridParam',{url:"server.php?agente="+$("#id_agente").val()+"&fechadia="+$("#id_date").val()}).trigger("reloadGrid");
		}
		jQuery(document).ready(function(){
			
			$(document).keypress(function(e){

			    if(e.keyCode == 38 || e.keyCode == 40)  //up/down arrow override
			    {
				var gridArr = $('#myGrid').getDataIDs();
				var selrow = $('#myGrid').getGridParam("selrow");
				var curr_index = 0;
				for(var i = 0; i < gridArr.length; i++)
				{
				    if(gridArr[i]==selrow)
				        curr_index = i;
				}

				if(e.keyCode == 38) //up
				{
				    if((curr_index-1)>=0)
				        $('#myGrid').resetSelection().setSelection(gridArr[curr_index-1],true);
				}
				if(e.keyCode == 40) //down
				{
				    if((curr_index+1)<gridArr.length)
				        $('#myGrid').resetSelection().setSelection(gridArr[curr_index+1],true);
				}
			    }

			});
			
			    
			jQuery("#myGrid").jqGrid({
			   	url:'server.php?nocarga=0',
				datatype: "xml",
			   	colNames:['Nombre Vendedor', 'Cliente', 'Socio','Estatus','Fecha'],
			   	colModel:[
			    		{name:'nombreagente',index:'nombreagente', width:400}, 
					{name:'cliente',index:'cliente', width:60, sorttype:"int"}, 
					{name:'socio',index:'socio', width:60, sorttype:"int"}, 
					{name:'estatus',index:'estatus', width:60, sorttype:"string"}, 
					{name:'fecha',index:'fecha', width:90, sorttype:"date", formatter:"date"}		
			   	],
			   	rowNum:10,
			   	autowidth: true,
			   	rowList:10,
				pager : '#gridpager',
			   	sortname: 'agente',
			    	sortorder: "desc",
				multiselect: true,
				footerrow: true,
				loadui: "block",
				height: "160px"
			});	

		});

	</script>
	<Strong><?php echo $date; ?></Strong>
	<br/>
	<table>
	<tr>
		<td>Agente:</td>
		<td><?php echo $combo_agentes; ?></td>
		<td><input type="button" onclick="Buscar(); return false;" value=" Buscar " /></td>
	</tr>
	</table>
	<br/>
	<table id="myGrid"></table>
	<div id="gridpager"></div>
	<input type="hidden" id="id_date" value="<?php echo $date; ?>" />

